package com.mgl.movesprite;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import com.mgl.movesprite.game.factory.GameObjectFactory;
import com.mgl.movesprite.game.factory.GameTextureManager;
import com.mgl.movesprite.game.model.Level;

public class MainGame extends SimpleBaseGameActivity {

	// constants
	private static int CAMERA_WIDTH = 480;
	private static int CAMERA_HEIGHT = 800;

	// fields
	private Camera camera;
	private GameTextureManager gameTextureManager = null;
	private GameObjectFactory gameObjectFactory = null; 

	@Override
	public EngineOptions onCreateEngineOptions() {
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);

	}

	@Override
	protected void onCreateResources() {

		gameTextureManager = new GameTextureManager(getTextureManager(),
				getAssets(), getVertexBufferObjectManager());
		gameTextureManager.loadTexture();

	}

	@Override
	protected Scene onCreateScene() {

		final Scene scene = new Scene();       
		//new Level(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, gameTextureManager.getBackgroundTexture(),getVertexBufferObjectManager(), gameTextureManager,scene);
		gameObjectFactory = new GameObjectFactory(gameTextureManager);
		final Level level =  new Level(scene,camera,gameObjectFactory);
		
		level.init();
		
         scene.registerUpdateHandler(new IUpdateHandler() {                    
             public void reset() {        
             }             
             public void onUpdate(float pSecondsElapsed) {
                 //HERE IS THE GAME LOOP
            	 //System.out.println("this is the time elapsed : "+pSecondsElapsed);
            	 level.update(pSecondsElapsed, level);
             }
         });
         
         

		return scene;
	}
}
